package com.bf.electroplating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.mapper.ComContingencyPlanMapper;
import com.bf.electroplating.pojo.dto.contingencyplan.ComContingencyPlanDTO;
import com.bf.electroplating.pojo.dto.contingencyplan.ComContingencyPlanQueryDTO;
import com.bf.electroplating.pojo.dto.pollution.PollutionPermitDTO;
import com.bf.electroplating.pojo.dto.pollution.PollutionPermitQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComContingencyPlan;
import com.bf.electroplating.pojo.entity.ComDischargePermit;
import com.bf.electroplating.pojo.enums.AttModuleEnums;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.contingencyplan.ComContingencyPlanVO;
import com.bf.electroplating.pojo.vo.pollution.PollutionPermitVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ComContingencyPlanService;
import com.bf.electroplating.service.PubAttachmentService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 15451
* @description 针对表【com_contingency_plan(应急预案表)】的数据库操作Service实现
* @createDate 2025-05-09 11:11:32
*/
@Service
public class ComContingencyPlanServiceImpl extends ServiceImpl<ComContingencyPlanMapper, ComContingencyPlan>
    implements ComContingencyPlanService{

	@Resource
	private ComCompanyService comCompanyService;

	@Resource
	private PubAttachmentService pubAttachmentService;

	@Override
	public ResultVO < Page < ComContingencyPlanVO > > pageList ( ComContingencyPlanQueryDTO queryDTO ) {
		Page < ComContingencyPlan > page = new Page <> ( queryDTO.getPageNum () , queryDTO.getPageSize () );
		Map < String,String > companyMap =
				comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId ,
						ComCompany::getCompanyName ) );

		LambdaQueryWrapper < ComContingencyPlan > wrapper = new LambdaQueryWrapper <> ();
		if ( StringUtils.isNotBlank ( queryDTO.getCompanyId () ) ) {
			wrapper.eq ( ComContingencyPlan::getCompanyId , queryDTO.getCompanyId () );
		}
		if ( StringUtils.isNotBlank ( queryDTO.getCompanyId () ) ) {
			wrapper.like ( ComContingencyPlan::getCompanyId , queryDTO.getCompanyId () );
		}
		wrapper.orderByDesc ( ComContingencyPlan::getCreateTime );

		Page < ComContingencyPlan > resultPage = this.page ( page , wrapper );

		// 转换为VO
		Page < ComContingencyPlanVO > voPage = new Page <> ();
		BeanUtils.copyProperties ( resultPage , voPage , "records" );

		List < ComContingencyPlanVO > voList = new ArrayList <> ();
		for ( ComContingencyPlan entity : resultPage.getRecords () ) {
			ComContingencyPlanVO vo = new ComContingencyPlanVO ();
			BeanUtils.copyProperties ( entity , vo );
			vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
			vo.setAttFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.CONTINGENCY_PLAN.getCode () ) );
			voList.add ( vo );
		}
		voPage.setRecords ( voList );

		return ResultVO.ok ( voPage );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > saveData ( ComContingencyPlanDTO dto ) {
		ComContingencyPlan comContingencyPlan = new ComContingencyPlan ();
		BeanUtils.copyProperties ( dto , comContingencyPlan );
		saveOrUpdate ( comContingencyPlan );

		pubAttachmentService.saveWholeAttachmentRel ( dto.getAttFile () , comContingencyPlan.getId () , AttModuleEnums.CONTINGENCY_PLAN.getCode () );

		return ResultVO.ok ( null , "保存成功" );
	}

	@Override
	@Transactional ( rollbackFor = Exception.class )
	public ResultVO < String > delete ( List < String > ids ) {
		if ( ids == null || ids.isEmpty () ) {
			return ResultVO.fail ( "ID不能为空" );
		}

		this.removeByIds ( ids );
		return ResultVO.ok ( null , "删除成功" );
	}

	@Override
	public ResultVO < ComContingencyPlanVO > getDetail ( String id ) {
		if ( StringUtils.isBlank ( id ) ) {
			return ResultVO.fail ( "ID不能为空" );
		}
		ComContingencyPlan entity = this.getById ( id );
		if ( entity == null ) {
			return ResultVO.fail ( "记录不存在" );
		}

		Map < String,String > companyMap = comCompanyService.list ().stream ().collect ( Collectors.toMap ( ComCompany::getId , ComCompany::getCompanyName ) );

		ComContingencyPlanVO vo = new ComContingencyPlanVO ();
		BeanUtils.copyProperties ( entity , vo );
		vo.setCompanyName ( companyMap.get ( entity.getCompanyId () ) );
		vo.setAttFile ( pubAttachmentService.getWholeFile ( entity.getId () , AttModuleEnums.CONTINGENCY_PLAN.getCode () ) );

		return ResultVO.ok ( vo );
	}

}




